from django.contrib.auth.hashers import make_password, check_password
from django.views import View
from django.http import JsonResponse
from . import models
from django.core.cache import cache
import time
from celery_task.async_task import send_email_task
# Create your views here.

class ResgiterView(View):
    #注册用户
    def post(self,request):
        name = request.POST.get('name')
        account = request.POST.get('account')
        password = request.POST.get('password')
        email = request.POST.get('email')
        obj = models.UserModel.objects.filter(account=account).first()
        if obj:
            return JsonResponse({'code':400,'msg':'账户已经存在了'})
        password = make_password(password)
        instance = models.UserModel.objects.create(name=name,account=account,password=password,email=email)
        return JsonResponse({'code':200,'msg':'注册用户成功'})

class LoginView(View):
    #用户登录
    def post(self,request):
        account = request.POST.get('account')
        password = request.POST.get('password')
        code = request.POST.get('code') #验证码
        email_code = cache.get(f'email_{account}')#发给邮箱的验证码.get(f'email_{account})
        print(code,email_code)
        if code and email_code:
            if code != email_code:
                return JsonResponse({'code':400,'msg':'验证码错误'})
        else:
            return JsonResponse({'code':400,'msg':'请先点击发送邮件获取验证码'})
        obj = models.UserModel.objects.filter(account=account).first()
        if not obj:
            return JsonResponse({'code':400,'msg':'当前用户不存在'})
        pwd_true = check_password(password,obj.password)
        response = JsonResponse({'code':200,'msg':'登录成功'})
        response.set_cookie('account',account)
        response.set_cookie('email',obj.email)
        if pwd_true:
            return response
        else:
            return JsonResponse({'code':400,'msg':'用户名或密码错误'})

class LoginSendEmailView(View):
    #用户登录前，需要验证码，发送验证码给用户的邮箱
    def post(self,request):
        account = request.POST.get('account')
        email = request.POST.get('email')
        code = str(time.time())[-5:]
        cache.set(f'email_{account}',code)
        print(cache.get(f'email_{account}'))
        res = send_email_task.delay(email,code)
        task_id = res.id
        print('验证码是',code)
        return JsonResponse({'code':200,'msg':f'请查看{email}邮箱中是否收到邮件','task_id':task_id})

class AllUserDataView(View):
    #查询cache_user_func定时任务执行时存到cache中的用户数据
    def get(self,request):
        key = 'all-user-data'
        data = cache.get(key)
        if data:
            return JsonResponse({'code':200,'data':data})
        else:
            return JsonResponse({'code':400,'msg':'没有相关数据'})

class AddFuncDataView(View):
    #查询add_func 定时任务执行时存到cache中的数据
    def get(self, request):
        data = cache.get('add_ret')
        print(data, type(data))
        return JsonResponse({'code': 200, 'data': data})

class UserTaskIdGetDataView(View):
    def get(self,request):
        from celery_task.check_task import check_task_status
        #检查任务是否成功了，获取任务的返回值
        task_id = request.GET.get('task_id')
        if not task_id:
            return JsonResponse({'code':400,'msg':'没有携带任务id'})
        ret = check_task_status(task_id)
        return JsonResponse(ret)

class TestCacheView(View):
    def get(self,request):
        name = request.GET.get('name')
        cache.set('name',name)
        print(cache.get('name'))
        return JsonResponse({'code':200,'msg':'设置成功'})
    def post(self,request):
        key = request.GET.get('key')
        name = cache.get(key)
        return JsonResponse({'code':200,'data':name})
    def put(self,request):
        from celery_task.async_task import search_user_task
        res = search_user_task.delay()
        task_id = res.id
        return JsonResponse({'code':200,'task_id':task_id})